package com.mobile.web.injuryDeclaration;

import com.mobile.service.dto.InjuryDeclarationDTO;
import com.mobile.service.dto.InjuryDeclarationQueryDTO;
import com.mobile.service.inter.InjuryDeclarationService;
import com.mobile.util.*;
import com.mobile.web.base.BaseController;
import com.mobile.util.config.aop.AutoFill;
import com.mobile.util.config.aop.OperateType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工伤保险申请表API接口
 */
@RestController
@RequestMapping("/injuryDeclaration")
@Tag(name = "工伤保险申请管理接口", description = "包含工伤保险申请的查询、新增、修改、删除等操作")
public class InjuryDeclarationController extends BaseController {

    @Autowired
    private InjuryDeclarationService injuryDeclarationService;

    @PostMapping("/addInjuryDeclaration")
    //@RequiresPermissions("injuryDeclarationmana:add:1")
    @Operation(summary = "创建工伤保险申请")
    @AutoFill(OperateType.INSERT)
    public Object addInjuryDeclaration(@Valid @RequestBody InjuryDeclarationDTO dto) {
        Map<String, Object> map;
        try {
            map = injuryDeclarationService.addInjuryDeclaration(dto);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping(value = "/getInjuryDeclarationListPage")
    //@RequiresPermissions("injuryDeclarationmana:read:1")
    @Operation(summary = "分页查询工伤保险申请")
    public Object getInjuryDeclarationListPage(@Validated InjuryDeclarationQueryDTO queryDTO) {
        Map<String, Object> map = new HashMap<>();
        try {
            PageResult<InjuryDeclarationDTO> page = injuryDeclarationService.getInjuryDeclarationListPage(queryDTO);
            map.put("page", page);
            map.put("data", page.getPageDatas());
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping(value = "/getInjuryDeclarationById")
    //@RequiresPermissions("injuryDeclarationmana:edit:1")
    @Operation(summary = "查询工伤保险申请详情")
    public Object getInjuryDeclarationById(
        @Parameter(description = "工伤保险申请主键", required = true) 
        @RequestParam("id") String id) {
        Map<String, Object> map = new HashMap<>();
        try {
            InjuryDeclarationDTO dto = injuryDeclarationService.getInjuryDeclarationById(id);
            if (StringUtil.isEmpty(dto)) {
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
                map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            } else {
                map.put("data", dto);
                map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            }
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @PostMapping("/updateInjuryDeclaration")
    //@RequiresPermissions("InjuryDeclarationmana:edit:1")
    @Operation(summary = "编辑工伤保险申请")
    @AutoFill(OperateType.UPDATE)
    public Object updateInjuryDeclaration(@Valid @RequestBody InjuryDeclarationDTO dto) {
        Map<String, Object> map;
        try {
            map = injuryDeclarationService.updateInjuryDeclaration(dto);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping("/removeInjuryDeclaration")
    //@RequiresPermissions("injuryDeclarationmana:delete:1")
    public Object removeInjuryDeclaration(@RequestParam("id") String id) {
        Map<String, Object> map;
        try {
            map = injuryDeclarationService.removeInjuryDeclaration(id);
        } catch (Exception e) {
            map = new HashMap<>();
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }

    @GetMapping("/getInjuryDeclarationList")
    //@RequiresPermissions("injuryDeclarationmana:read:1")
    @Operation(summary = "条件查询工伤保险申请列表")
    public Object getInjuryDeclarationList(@Valid  InjuryDeclarationQueryDTO dto) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<InjuryDeclarationDTO> list = injuryDeclarationService.getInjuryDeclarationList(dto);
            map.put("data", list);
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        } catch (Exception e) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0016"));
            logger.error(e.getMessage(), e);
        }
        return AppUtil.returnObject(map);
    }
}
